<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Asset Manager
 *
 * Software to automatically manage how assets are delivered on a website
 *
 * @package		Asset Manager
 * @author		Adam Lewis <adam.lewis@bestservedcold.com>
 * @copyright	Copyright (c) 2006 - 2012, Best Served Cold Media Limited.
 * @license		http://bestservedcold.com/license.html
 * @link		http://bestservedcold.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Assets Library
 *
 * The Asset library simply checks the asset database and returns the asset location.  If the asset
 * is not in the database, then it calls the asset library and queues the asset for processing.
 *
 * @package		Asset Manager
 * @subpackage	Assets
 * @category	Libraries
 * @author  	Adam Lewis <adam.lewis@bestservedcold.com>
 * @since		2012-05-22
 */
require_once(APPPATH.'libraries/asset_manager/am_handler.php');
class Asset_manager_queue extends Am_handler
{
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Process Queue
	 *
	 * Simply get's the queue and process it
	 *
	 * @access		public
	 * @since		2012-05-25
	 * @return		void
	 */
	public function process_queue()
	{
		foreach($this->get_queue()->result_array() as $asset)
		{
			$this->process_asset($asset);
		}
	}

	/**
	 * Delete Queued Asset
	 *
	 * Removes the asset from the queue once we've processed it
	 *
	 * @access		private
	 * @since		2012-05-25
	 * @param		integer		$asset_id
	 * @return		boolean
	 */
	private function delete_queued_asset($asset_id)
	{
		return $this->CI->db->delete($this->CI->config->item('queue_table', 'asset_manager'),
			array('id' => $id));
	}


	/**
	 * Get Queue
	 *
	 * Returns the queue from the database
	 *
	 * @access		private
	 * @since		2012-05-25
	 * @return		object
	 */
	private function get_queue()
	{
		return $this->CI->db->from($this->CI->config->item('queue_table', 'asset_manager'))->get();
	}


	/**
	 * Queue Asset
	 *
	 * Sticks an asset in the queue to process later
	 *
	 * @access		private
	 * @since		2012-05-29
	 * @param 		array		$asset
	 * @return		boolean
	 */
	private function queue_asset($asset)
	{
		return $this->CI->db->insert($this->CI->config->item('queue_table', 'asset_manager'), $asset);
	}
}